package com.contact.util.util.filters;

import java.util.EnumMap;
import java.util.List;
import java.util.Map;

import org.joda.time.LocalDate;

import com.contact.util.util.collections.Utils;

/**
 * This filter allows users to choose from a list of dates relative to a "baseDate".. The Display value is 
 * the selected Enum while the filter value is a DateRange generated from the display value.
 */
public class DateDataFilter<E extends Enum<E>> extends AbstractDataFilter<E, DateRange> {

    private static final long serialVersionUID = 259959499564511402L;

    private List<E> values = null;
    private LocalDate filterDate = null;
    private LocalDate displayDate = null;

    public DateDataFilter(String key, LocalDate filterDate, List<E> values) {
        this(key, filterDate, filterDate, values);
    }

    public DateDataFilter(String key, LocalDate filterDate, LocalDate displayDate, List<E> values) {
        super(DataFilterType.DATE, key);
        setFilterDate(filterDate);
        setDisplayDate(displayDate);
        setValues(values);
    }

    public final LocalDate getDisplayDate() {
        return displayDate;
    }

    public final void setDisplayDate(LocalDate displayDate) {
        this.displayDate = displayDate;
    }

    public final LocalDate getFilterDate() {
        return filterDate;
    }

    public final void setFilterDate(LocalDate baseDate) {
        this.filterDate = baseDate;
    }

    public List<E> getValues() {
        return values;
    }

    public final void setValues(List<E> values) {
        this.values = values;
    }

    @SuppressWarnings("unchecked")
    public final Map<E, Object> getSelectionMap() {

        if (Utils.isEmpty(values)) {
            return null;
        }
        Map<E, Object> map = new EnumMap<E, Object>((Class<E>) values.get(0).getClass());
        for (E dateRangeEnum : values) {
            String resourceKey = dateRangeEnum.toString().replace("_", "");
            map.put(dateRangeEnum, String.format("model_%s.%s.label",  dateRangeEnum.getClass().getSimpleName(), resourceKey));
        }
        return map;
    }
}
